package bll

import (
	"context"

	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/model"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
	"github.com/google/wire"
)

// LikeSet 注入Like
var LikeSet = wire.NewSet(wire.Struct(new(Like), "*"))

// Like 点赞收藏管理
type Like struct {
	LikeModel               *model.Like
	VideoModel              *model.Video
	GroupVideoModel         *model.GroupVideo
	GroupNotificationServer *GroupNotification
}

// Query 查询数据
func (a *Like) Query(ctx context.Context, params schema.LikeQueryParam, opts ...schema.LikeQueryOptions) (*schema.LikeQueryResult, error) {
	return a.LikeModel.Query(ctx, params, opts...)
}

// GetFirst 查询数据
func (a *Like) GetFirst(ctx context.Context, params schema.LikeQueryParam, opts ...schema.LikeQueryOptions) (*schema.Like, error) {
	item, err := a.LikeModel.GetFirst(ctx, params, opts...)
	if err != nil {
		return nil, err
	}
	if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Get 查询指定数据
func (a *Like) Get(ctx context.Context, id string, opts ...schema.LikeQueryOptions) (*schema.Like, error) {
	item, err := a.LikeModel.Get(ctx, id, opts...)
	if err != nil {
		return nil, err
	} else if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Create 创建数据
func (a *Like) Create(ctx context.Context, item schema.Like) (int, error) {

	item.ID = uuid.NewID()
	return a.SetLike(ctx, item), nil

}

// Update 更新数据
func (a *Like) Update(ctx context.Context, id string, item schema.Like) error {
	_, err := a.Get(ctx, id)
	if err != nil {
		return err
	}

	return a.LikeModel.Update(ctx, id, item)
}

// Delete 删除数据
func (a *Like) Delete(ctx context.Context, id string) error {
	item, err := a.LikeModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.LikeModel.Delete(ctx, id)
}
